<?php

class Utilitarios_ConexaoController extends Zend_Rest_Controller {

	public function init() {
		/* Initialize action controller here */
		$ajaxContext = $this->_helper->contextSwitch();
		$ajaxContext->setAutoJsonSerialization(true);
		$ajaxContext
		->addActionContext('index', array('json'))
		->addActionContext('get', array('json'))
		->addActionContext('post', array('json'))
		->addActionContext('put', array('json'))
		->addActionContext('delete', array('json'))
		->initContext('json');
	}

	public function indexAction(){
		$token=$_COOKIE['token'];
		$model_conexao=new Application_Model_Utilitarios_Conexao();
		$conexao=$model_conexao->index(array(array('property'=>'token','value'=>$token)));
		if($conexao['total']==1){
			$conexao['data']=$conexao['data'][0];
		}else{
			$conexao['data']=array();
			$conexao['success']=false;
			$conexao['message']='Conexão fechada pelo servidor!';
		}
		print Zend_Json::encode($conexao);
		exit;
	}

	public function getAction(){
		$model_conexao=new Application_Model_Utilitarios_Conexao();
		$conexao=$model_conexao->get($this->getRequest()->getParam('id'));
		print Zend_Json::encode($conexao);
		exit;
	}

	public function postAction(){
		$login=$this->getRequest()->getPost('login');
		$password=$this->getRequest()->getPost('password');

		$model_usuario=new Application_Model_Utilitarios_Usuario();
		$usuario=$model_usuario->index(array(array('property'=>'login','value'=>$login),array('property'=>'senha','value'=>$password)));

		if($usuario['total']==1){
			$conexao=array('usuario'=>$usuario['data'][0]['id'],'ip'=>'localhost');
			$model_conexao=new Application_Model_Utilitarios_Conexao();
			$conexao=$model_conexao->post($conexao);
			$conexao['data']['usuario']=$usuario['data'][0];
			$this->view->success=true;
			$this->view->mensagem='Bem vindo '.$conexao['data']['usuario']['nome'];
			$this->view->data=$conexao['data'];
		}else{
			$this->view->success=false;
			$this->view->mensagem=array('status'=>403,'msg'=>'Usuário e senha inválidos');
		}
	}

	public function putAction(){

	}

	public function deleteAction(){

	}

}